<?php
	class Image{
		private $name;
		private $description;
		private $albumId;
				
		private function checkAndChangeName($name) {
			$allFileNames = scandir('images');
			for($i = 2; $i < count($allFileNames); $i++) {
				if($allFileNames[$i] == $name) {
					$nameNoExt = substr($name, 0, strpos($name, '.'));
					$nameNoExt .= "_" . date('Y-m-d_H-i-s');
					$name = $nameNoExt . substr($name, strpos($name, '.'));
				}
			}
			return $name;
		}
		
		public function uploadImgToFileSystem() {
			$fileTempName = $_FILES["img"]["tmp_name"];
			$this->setName($_FILES["img"]["name"]);
			$fileType = $_FILES["img"]["type"];
			$this->setDescr($_POST['descr']);
	
			if(is_uploaded_file($fileTempName)) {
				if(substr($fileType, 0, 6) == "image/") {
					if(move_uploaded_file($fileTempName, "images/$this->name")) {
						echo "<p> Image successfully uploaded </p>";
					}
					else {
						echo "<p> There was a problem uploading the image </p>";
					}
				}
				else {
					echo "<p> Please choose an IMAGE! </p>";
				}
			}
		
		}
		
		private function setName($name) {
			$this->name = $this->checkAndChangeName($name);
		} 
		
		private function setDescr($description) {
			$this->description = $description;
		}
		
		public function setAlbumId($albumId) {
			$this->albumId = $albumId;
		}

		public function getName(){
			return $this->name;
		}
		public function getDescr(){
			return $this->description;
		}
		public function getAlbumId(){
			return $this->albumId;
		}
	}
	
	
?>